/**
 * 获取容器
 * @param room
 * @param full
 * @returns {*}
 */
const {wallBlood} = require("./Param");
/**
 * 查看容器
 * @param room
 * @param full
 * @returns {*}
 */
module.exports.findStore = function (room, full = 0) {
    return room.find(FIND_STRUCTURES).filter(structure => {
        switch (full) {
            case 0://获取任意存储
                return structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE;
            case 1://获取有资源存储
                return (structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE || structure.structureType === STRUCTURE_TERMINAL) && structure.usedStore() > 0;
            case -1://获取有空余存储
                return (structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE || structure.structureType === STRUCTURE_TERMINAL) && structure.freeStore() > 0;
        }
    });
}

/**
 * 查看容器
 * @param room
 * @param full
 * @returns {*}
 */
module.exports.findDefaultStore = function (room, full = 0) {
    return room.find(FIND_STRUCTURES).filter(structure => {
        switch (full) {
            case 0://获取任意存储
                return structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE;
            case 1://获取有资源存储
                return (structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE)  && structure.usedStore() > 0;
            case -1://获取有空余存储
                return (structure.structureType === STRUCTURE_CONTAINER || structure.structureType === STRUCTURE_STORAGE) && structure.freeStore() > 0;
        }
    });
}
/**
 *  查看中枢
 * @type {function(*, *=): *}
 */
module.exports.findLink = findLink = (room, full = 0) => {
    return room.find(FIND_STRUCTURES).filter((structure) => {
        switch (full) {
            case 0://获取任意存储
                return structure.structureType === STRUCTURE_LINK;
            case 1://获取有资源存储
                return structure.structureType === STRUCTURE_LINK && structure.freeStore() === 0;
            case -1://获取有空余存储
                return structure.structureType === STRUCTURE_LINK && structure.freeStore() > 0;
        }
    });
}

/**
 * 获取建造中的建筑
 * @param room
 * @returns {*}
 */
module.exports.findBuilding = function (room) {
    return room.find(FIND_MY_CONSTRUCTION_SITES);
}

/**
 * 获取需要维修的建筑
 * @param room
 * @returns {*}
 */
module.exports.findRepair = function (room) {
    return room.find(FIND_STRUCTURES, {
        filter: structure => {
            if(room.storage&&room.storage.usedStore()>100000&&room.controller.level===8){
                return (structure.structureType === STRUCTURE_RAMPART && structure.hits < wallBlood) || (structure.structureType === STRUCTURE_WALL && structure.hits < wallBlood*2)
                || (structure.hitsMax - structure.hits) > 500 && (structure.structureType !== STRUCTURE_RAMPART && structure.structureType !== STRUCTURE_WALL);
            }
            return (
                (structure.structureType === STRUCTURE_RAMPART && structure.hits < wallBlood / 2) || (structure.structureType === STRUCTURE_WALL && structure.hits < wallBlood)
                || (structure.hitsMax - structure.hits) > 500 && (structure.structureType !== STRUCTURE_RAMPART && structure.structureType !== STRUCTURE_WALL));
        },
    })
}
/**
 * 查找旗帜
 * @param category
 * @param number
 * @returns {Flag[]|Flag}
 */
module.exports.findFlagList = function (category, number = 0) {
    let flags = Object.values(Game.flags);
    let color;
    switch (category) {
        case "resource":
            color = COLOR_YELLOW;
            break;
        case "attack":
            color = COLOR_RED;
            break;
        case "attack_defense":
            color = COLOR_GREY;
            break;
        case "occupy":
            color = COLOR_PURPLE;
            break;
        case "reserve":
            color = COLOR_BLUE;
            break;
        case "resource_move":
            color = COLOR_ORANGE;
            break;
        case "guard":
            color = COLOR_BROWN;
            break;
        case "dismantle":
            color = COLOR_WHITE;
            break;
        case "stop":
            color = COLOR_GREEN;
            break;
    }
    if (number === 0) {
        return flags.filter(flag => flag.color === color);
    }
    return (flags.filter(flag => flag.color === color)).pop();
}

/**
 * 获取需要补充能量的塔
 * @param room
 * @param full
 * @returns {*}
 */
module.exports.findTower = function findTower(room, full = 0) {
    return room.find(FIND_MY_STRUCTURES, {
        filter: structure => {
            switch (full) {
                case 0://获取任意存储
                    return structure.structureType === STRUCTURE_TOWER;
                case 1://获取有资源存储
                    return structure.structureType === STRUCTURE_TOWER && structure.freeStore() === 0;
                case -1://获取有空余存储
                    return structure.structureType === STRUCTURE_TOWER && structure.freeStore() > 200;
            }
        }
    })
}
module.exports.findExtension = function findExtension(room, full = 0) {
    return room.find(FIND_MY_STRUCTURES, {
        filter: structure => {
            switch (full) {
                case 0://获取任意存储
                    return structure.structureType === STRUCTURE_EXTENSION;
                case 1://获取有资源存储
                    return structure.structureType === STRUCTURE_EXTENSION && structure.freeStore() === 0;
                case -1://获取有空余存储
                    return structure.structureType === STRUCTURE_EXTENSION && structure.freeStore() > 0;
            }
        }
    })
}
/**
 * 房间creep查找
 * @param room
 * @param role
 * @returns {*}
 */
module.exports.findRoleCreepsByRoom = function (room, role) {
    return room.find(FIND_MY_CREEPS).filter(creep => {
        return creep.memory.role === role;
    })
}
/**
 * 全部creep查找
 * @param role
 * @returns {Creep[]}
 */
module.exports.findRoleCreepsByAll = function (role) {
    return Object.values(Game.creeps).filter(creep => {
        return creep.memory.role === role;
    })
}

module.exports.findFreeMainSite = (room) => {
    return room.find(FIND_MY_STRUCTURES, {
        filter: site => {
            return (site.structureType === STRUCTURE_SPAWN || site.structureType === STRUCTURE_EXTENSION || site.structureType === STRUCTURE_TOWER || (site.structureType === STRUCTURE_NUKER && room.storage.usedStore() > 30000)) && (site.store && (site.freeStore() > 80 || site.freeStore() > site.maxStore() * 0.2));
        }
    })
}

/**
 * 查找最近的集合位置,并绑定
 * @param form
 * @param findList
 * @returns {undefined|*}
 */
module.exports.bestFindClose = function (form, findList) {
    if (findList.length <= 0) {
        return undefined;
    }
    //随机查找靠近自己的
    let closeList1 = form.pos.findInRange(findList, 1);
    if (closeList1.length > 0) {
        return closeList1[0];
    }
    //随机查找稍微大一点范围
    let closeList2 = form.pos.findInRange(findList, 5);
    if (closeList2.length > 0) {
        return closeList2[Math.floor(Math.random() * closeList2.length)];
    }
    //找最近的
    let closeList3 = form.pos.findInRange(findList, 8);
    if (closeList3.length > 0) {
        return form.pos.findClosestByRange(closeList3);
    }
    //全图找最近的
    return form.pos.findClosestByRange(findList);
}
/**
 * 获取房间内资源数量
 * @param resource
 * @param room
 * @returns {number}
 */
module.exports.findResource = function (resource, room) {
    let number = 0;
    if (room.terminal) {
        number += room.terminal.usedStore(resource);
    }
    if (room.storage) {
        number += room.storage.usedStore(resource);
    }
    return number;
}

module.exports.findNuker = function (room) {
    return room.find(FIND_MY_STRUCTURES, {
        filter: site => {
            return site.structureType === STRUCTURE_NUKER;
        }
    })[0]
}